﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.ComponentModel;

namespace AdministrationWeb.Administration
{
    public partial class EditPathways : System.Web.UI.Page
    {

        private const string CONNECTION_STRING_NAME = "ApplicationConnectionString";
        
        private const string INSET_PATHWAY = "INSERT INTO lkup_pathway (pathway_name, focus_id) "
                                           + "VALUES(?pathwayName, ?focusAreaId) ";

        protected void Page_Load(object sender, EventArgs e)
        {
            ddlFocusArea.Focus();
            lblMessage.Text = "";
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            //create connection
           // MySqlConnection conn = new MySqlConnection(getConnectionString());
            //MySqlCommand command = new MySqlCommand(INSET_PATHWAY, conn);

            //add parameters
            //command.Parameters.AddWithValue("?pathwayName", txtNewName.Text.ToUpper());
            //command.Parameters.AddWithValue("?focusAreaId", ddlFocusArea.SelectedValue);

            try
            {

                SqlPathway.InsertParameters["pathway_name"].DefaultValue = txtNewName.Text;
                SqlPathway.InsertParameters["focus_id"].DefaultValue = ddlFocusArea.SelectedValue;

                int numRows = SqlPathway.Insert();
                //connect and run sql
                //conn.Open();
                //int numRows = command.ExecuteNonQuery();

                if (numRows > 0)
                {
                    txtNewName.Text = "";
                    gdvPathway.DataBind();
                    lblMessage.Text = "Pathway succesfuly created";
                }
                else
                {
                    lblMessage.Text = "Unable to create pathway, please try again";
                }

            }catch (Exception ex)
            {
                lblMessage.Text = "Error creating pathway: " + ex.Message;
            }

        }

        private static string getConnectionString()
        {
            return ConfigurationManager.ConnectionStrings[CONNECTION_STRING_NAME].ConnectionString;
        }

        protected void gdvPathway_DataBound(object sender, EventArgs e)
        {            
            
            //disable delete function if mentors are attached to pathway
            foreach (GridViewRow gvr in gdvPathway.Rows)
            {
                if (gvr.Cells[2].Text != "" && gvr.Cells[2].Text != null)
                {
                    if (Convert.ToInt32(gvr.Cells[2].Text) > 0)
                    {
                        gvr.Cells[4].Enabled = false;
                        gvr.Cells[4].Controls[0].Visible = false;
                        gvr.Cells[4].Text = "N/A";
                    }
                }
            }
        }

        protected void gdvPathway_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            //test for and display error or succes
            if (e.Exception != null)
            {
                lblMessage.Text = "Error updating pathway: " + e.Exception.Message;
                e.ExceptionHandled = true;
                e.KeepInEditMode = true;
            }
            else if (e.AffectedRows == 0)
            {
                lblMessage.Text = "Unknown error, pathway not updated, try again";
            }
            else
            {
                lblMessage.Text = "Pathway updated succesfuly";
            }
        }

        protected void gdvPathway_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            //test for and display error or succes
            if (e.Exception != null)
            {
                lblMessage.Text = "Error Deleting pathway: " + e.Exception.Message;
                e.ExceptionHandled = true;
            }
            else if (e.AffectedRows == 0)
            {
                lblMessage.Text = "Unknown error, pathway not deleted, try again";
            }
            else
            {
                lblMessage.Text = "Pathway deleted succesfuly";
            }
        }

    }
}